package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //Person(String,int)
        Person p = new Person("Lj", 23);
        System.out.println(p);

        Class cls = Class.forName("reflect.Person");
        //通过类对象获取器表示的类的某个指定的构造器
        //cls.getConstructor();//Person()
        /*
            Constructor类的每一实例用于表示某个类上的某一个构造器
         */
        Constructor c = cls.getConstructor(String.class, int.class);//调用某类上的某个构造器
        Object o = c.newInstance("LJ", 23);//等同于:new Person("LJ",23)
        System.out.println(o);
    }
}
